import com.alibaba.alimonitor.jmonitor.utils.StringUtils
import com.alibaba.moco.RedisPoolInstance
import com.alibaba.moco.util.BaseTypeUtil
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import redis.clients.jedis.Jedis
import script.GlobalValues
import script.moco.dto.AcceptOrderRequest
import script.moco.dto.AcceptOrderResponse
import script.moco.dto.OrderInfo
import script.v1.didiCallback.NoticeCallback
import script.v1.didiCallback.NotifyRequest
//模拟司机接单
def excute(data) {

    def slurper = new JsonSlurper();
    Jedis redis = RedisPoolInstance.getRedisClient();

    def params = data.getParameterMap();
    def request = BaseTypeUtil.mapToBean(params, AcceptOrderRequest.class) as AcceptOrderRequest;//请求数据转换为对象

    def response = new AcceptOrderResponse();//定义响应对象

    def jsonStr = redis.get(GlobalValues.KEY_ORDER + request.getOrderId());//从 Redis中获取 json 订单字符串

    if (jsonStr == null) {//验证订单是否存在
        response.setSuccess("false");
        response.setErrorMsg("订单id不存在 !");
        response.setErrorCode(39001);
        response.setErrorLevel(1);

        return response;

    }
    System.out.println(jsonStr)

    def detailMap = slurper.parseText(jsonStr);    //从json字符串变成json对象

    if (!detailMap.containsKey("order")) {
        response.setSuccess("false");
        response.setErrorMsg("订单不存在 !");
        response.setErrorCode(39001);
        response.setErrorLevel(1);

        return response;
    }

    def jsonMap = detailMap.get("order");
    def taxiObj = BaseTypeUtil.jsonToBean(jsonMap, OrderInfo.class) as OrderInfo;//将json对象转换为TaxiRequest对象

    //司机接单完善数据
    taxiObj.setStatus(400);
    taxiObj.setSub_status(4001);
    taxiObj.setId(request.getOrderId());//订单ID
    taxiObj.setDriver_name(request.getDriverName());//司机名称
    taxiObj.setDriver_phone(request.getDriverPhone());//司机号码
    taxiObj.setDriver_card(request.getDriverLicense());    //司机车牌号码
    taxiObj.setDlng(request.getDriverLongitude());//司机经度
    taxiObj.setDlat(request.getDriverLatitude());//司机纬度
    taxiObj.setDriver_level(String.valueOf(request.getDriverComment()));//司机评价
    //taxiObj.setDriver_goodCount(request.getDriverGoodCount()); //司机好评数
    //taxiObj.setDriver_geotype(request.getDriverGeotype());//司机坐标类型
    taxiObj.setDriver_car_type(request.getDriverCarType());    //司机车型

    detailMap.put("order", taxiObj)

    redis.set(GlobalValues.KEY_ORDER + taxiObj.getId(), JsonOutput.toJson(detailMap).toString())    //直接覆盖原有数据，进行修改

    //通知回调
    NoticeCallback noticeCallbackTool = new NoticeCallback();
    NotifyRequest notifyRequest = new NotifyRequest();
    notifyRequest.setOrderId(taxiObj.getId());
    notifyRequest.setNotifyType(1);
    notifyRequest.setNotifyDesc("订单中间状态流转[司机接单(等待接驾)]");
    String url = noticeCallbackTool.getCallbackUrlFromRequest(data)
    if (StringUtils.isNotBlank(url)) {
        response.setSuccess("true");
        response.setErrorMsg(null);
        response.setErrorCode(0);
        response.setErrorLevel(0);
        response.setContent(noticeCallbackTool.handNotify(url, notifyRequest));
    } else {
        response.setSuccess("false");
        response.setErrorMsg("session 中不存在callback地址");
        response.setErrorCode(-1);
        response.setErrorLevel(0);
    }
    return response;
}